package com.yuyue.springFramework.note01;

import com.yuyue.springFramework.note01.entity.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 《Spring-1 初步认识》文章代码
 *  文章地址：https://mp.weixin.qq.com/s/ZOlZiK0npsnQ5_zlmNqyHA
 */
public class Note1Test {
    @Test
    //使用IOC容器获取对象
    public void fun(){
        //获取容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext01.xml");
        //从容器获取对象
        ClassEntity classEntity = applicationContext.getBean("classEntity", ClassEntity.class);

        classEntity.setName("IOC");
        classEntity.setAge(18);

        System.out.println(classEntity);
    }

    @Test
    //内部类、静态工厂、实例工厂
    public void fun1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext01.xml");
        //重复从实例工厂获取
        OneClass oneClass = applicationContext.getBean("oneClassFromFactory", OneClass.class);
        OneClass oneClass2 = applicationContext.getBean("oneClassFromFactory", OneClass.class);
    }

    @Test
    //依赖注入
    public void fun2(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext01.xml");
        ArgsEntity argsEntity1 = applicationContext.getBean("argsEntity1", ArgsEntity.class);
        ArgsEntity argsEntity2 = applicationContext.getBean("argsEntity2", ArgsEntity.class);

        System.out.println(argsEntity1);
        System.out.println(argsEntity2);
    }

    @Test
    //集合注入
    public void fun3(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext01.xml");
        CollectionsEntity collectionsEntity = (CollectionsEntity)applicationContext.getBean("collectionsEntity");

        System.out.println(collectionsEntity);
        //空值验证
        System.out.println(null == collectionsEntity.getObjArr()[2]);//true
        System.out.println("".equals(collectionsEntity.getObjArr()[3]));//true
        System.out.println("".equals(collectionsEntity.getObjArr()[4]));//false
        System.out.println(collectionsEntity.getObjArr()[4].toString().length());//2
    }

    @Test
    //父bean和集合的合并
    public void fun4(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext01.xml");
        CollectionsEntity collectionsEntity = (CollectionsEntity)applicationContext.getBean("collectionsEntity");
        System.out.println(collectionsEntity);
        System.out.println("-------------------------------");
        CollectionsEntity childCollectionsEntity = (CollectionsEntity)applicationContext.getBean("childCollectionsEntity");
        System.out.println(childCollectionsEntity);
    }

    @Test
    //p 命名空间
    public void fun5() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext01.xml");
        ArgsEntity argsEntity = applicationContext.getBean("argsEntity3", ArgsEntity.class);
        System.out.println(argsEntity);
    }

    @Test
    //c 命名空间
    public void fun6() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext01.xml");
        ArgsEntity argsEntity = applicationContext.getBean("argsEntity4", ArgsEntity.class);
        System.out.println(argsEntity);
    }

    @Test
    //单例获取多例对象，lookup-method
    public void fun7(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext01.xml");
        SingleClass singleClass = applicationContext.getBean("singleClass", SingleClass.class);
        singleClass.process();
        singleClass.process();//查看两次调用是否是不同的实例
    }

    @Test
    //java、xml、注解三种实现方式先后顺序对比
    public void fun8(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext01.xml");
        Lifecycle lifecycle = applicationContext.getBean("lifecycle", Lifecycle.class);
        //销毁容器
        ((ClassPathXmlApplicationContext) applicationContext).close();
    }
}